﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml.XPath;
using uREST.Blog.Extensions;
using uREST.Core.Attributes;
using uREST.Core.Handlers;
using OpenRasta.Web;
using Umlaut.Umb.Blog;

namespace uREST.Blog.Handlers
{
    [UmbracoAuthorize(Applications = "content")]
    public class CommentHandler : AbstractHandler
    {
        public CommentHandler(ICommunicationContext context)
            : base(context)
        { }

        // GET (LIST)
        [HttpOperation(ForUriName = "CommentList")]
        public OperationResult GetCommentList(
            [Optional] int blogId,
            [Optional] int postId)
        {
            XPathNodeIterator comments = null;

            if(blogId > 0)
            {
                comments = BlogLibrary.GetCommentsForBlog(blogId);
            }
            else if(postId > 0)
            {
                comments = BlogLibrary.GetCommentsForPost(postId);
            }
            else
            {
                comments = BlogLibrary.GetComments();
            }

            if (comments == null)
                return new OperationResult.BadRequest();

            return new OperationResult.OK { ResponseResource = comments.ToCommentResourceList() };
        }
    }
}
